﻿using System;
using System.Windows.Forms;

namespace MyControls
{
    public partial class PTCom : UserControl
    {
        private int type = -1;
        private int level = -1;
        private int open = 1;
        private int STT = -1;

        string originList = "";
        string list = "";
        string answer = "";

        public int Type
        {
            set { type = value; }
            get { return type; }
        }

        public int Level
        {
            set { level = value; }
            get { return level; }
        }

        public int Open
        {
            set { open = value; }
            get { return open; }
        }

        public string OriginList
        {
            get { return originList; }
        }
        public string List
        {
            get { return list; }
        }
        public string Answer
        {
            get { return answer; }
        }
        
        public PTCom()
        {
            InitializeComponent();
            Reset();
            SetInvisible();
        }

        private void SetInvisible()
        {
            this.txtKetQua1.Visible = false;
            this.txtKetQua2.Visible = false;
            this.txtSoBiChia1.Visible = false;
            this.txtSoBiChia2.Visible = false;
            this.txtSoChia1.Visible = false;
            this.txtSoChia2.Visible = false;
            this.txtSoHang11.Visible = false;
            this.txtSoHang12.Visible = false;
            this.txtSoHang22.Visible = false;
            this.txtSoHang21.Visible = false;
            this.txtThuong1.Visible = false;
            this.txtThuong2.Visible = false;
            this.Line1.Visible = false;
            this.Line2.Visible = false;
            this.LineChia21.Visible = false;
            this.LineChia22.Visible = false;
            this.LineChia11.Visible = false;
            this.LineChia12.Visible = false;
        }

        private void ShowPhepCong1()
        {
            this.txtKetQua1.Visible = true;
            this.txtSoHang11.Visible = true;
            this.txtSoHang12.Visible = true;
            this.Line1.Visible = true;
        }

        private void ShowPhepCong2()
        {
            this.txtKetQua2.Visible = true;
            this.txtSoHang21.Visible = true;
            this.txtSoHang22.Visible = true;
            this.Line2.Visible = true;
        }

        private void ShowPhepChia1()
        {
            this.txtThuong1.Visible = true;
            this.txtSoChia1.Visible = true;
            this.txtSoBiChia1.Visible = true;
            this.LineChia11.Visible = true;
            this.LineChia12.Visible = true;
        }

        private void ShowPhepChia2()
        {
            this.txtThuong2.Visible = true;
            this.txtSoChia2.Visible = true;
            this.txtSoBiChia2.Visible = true;
            this.LineChia21.Visible = true;
            this.LineChia22.Visible = true;
        }

        public void ShowAll()
        {
            switch (Type)
            {
                case 0:
                    ShowPhepCong1();
                    ShowPhepCong2();
                    break;
                case 1:
                    ShowPhepCong1();
                    ShowPhepCong2();
                    break;
                case 2:
                    ShowPhepCong1();
                    ShowPhepCong2();
                    break;
                case 3:
                    ShowPhepChia1();
                    ShowPhepChia2();
                    break;
            }
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Reset();
            this.Type = cbType.SelectedIndex;
            this.SetInvisible();
            switch (Type)
            {
                case 0 :
                    ShowPhepCong1();
                    this.btnClone.Enabled = true;
                    break;
                case 1:
                    ShowPhepCong1();
                    this.btnClone.Enabled = true;
                    break;
                case 2:
                    ShowPhepCong1();
                    this.btnClone.Enabled = true;
                    break;
                case 3:
                    ShowPhepChia1();
                    this.btnClone.Enabled = true;
                    break;
            }
        }

        private void btnClone_Click(object sender, EventArgs e)
        {
            switch (Type)
            {
                case 0:
                    ShowPhepCong2();
                    CopyData();
                    break;
                case 1:
                    ShowPhepCong2();
                    CopyData();
                    break;
                case 2:
                    ShowPhepCong2();
                    CopyData();
                    break;
                case 3:
                    ShowPhepChia2();
                    CopyData();
                    break;
            }
        }

        private void Reset()
        {
            type = -1;
            level = -1;
            open = 1;
            originList = "";
            list = "";
            answer = "";
            this.txtKetQua1.Text = "";
            this.txtKetQua2.Text = "";
            this.txtSoBiChia1.Text = "";
            this.txtSoBiChia2.Text = "";
            this.txtSoChia1.Text = "";
            this.txtSoChia2.Text = "";
            this.txtSoHang11.Text = "";
            this.txtSoHang12.Text = "";
            this.txtSoHang21.Text = "";
            this.txtSoHang22.Text = "";
            this.txtThuong1.Text = "";
            this.txtThuong2.Text = "";
            this.btnClone.Enabled = false;
            SetInvisible();
        }

        private void CopyData()
        {
            this.txtKetQua1.Text = this.txtKetQua1.Text.Replace(" ", "");
            this.txtKetQua2.Text = this.txtKetQua1.Text;

            this.txtSoBiChia1.Text = this.txtSoBiChia1.Text.Replace(" ", "");
            this.txtSoBiChia2.Text = this.txtSoBiChia1.Text;

            this.txtSoChia1.Text = this.txtSoChia1.Text.Replace(" ", "");
            this.txtSoChia2.Text = this.txtSoChia1.Text;

            this.txtSoHang11.Text = this.txtSoHang11.Text.Replace(" ", "");
            this.txtSoHang21.Text = this.txtSoHang11.Text;

            this.txtSoHang12.Text = this.txtSoHang12.Text.Replace(" ", "");
            this.txtSoHang22.Text = this.txtSoHang12.Text;

            this.txtThuong1.Text = this.txtThuong1.Text.Replace(" ", "");
            this.txtThuong2.Text = this.txtThuong1.Text;
        }

        public bool CheckValidateData()
        {
            this.txtKetQua1.Text = this.txtKetQua1.Text.Replace(" ", "");
            this.txtKetQua2.Text = this.txtKetQua2.Text.Replace(" ", "");
            if (this.txtKetQua1.Text.Length != this.txtKetQua2.Text.Length)
                return false;

            this.txtSoBiChia1.Text = this.txtSoBiChia1.Text.Replace(" ", "");
            this.txtSoBiChia2.Text = this.txtSoBiChia2.Text.Replace(" ", "");
            if (this.txtSoBiChia1.Text.Length != this.txtSoBiChia2.Text.Length)
                return false;

            this.txtSoChia1.Text = this.txtSoChia1.Text.Replace(" ", "");
            this.txtSoChia2.Text = this.txtSoChia2.Text.Replace(" ", "");
            if (this.txtSoChia1.Text.Length != this.txtSoChia2.Text.Length)
                return false;

            this.txtSoHang11.Text = this.txtSoHang11.Text.Replace(" ", "");
            this.txtSoHang21.Text = this.txtSoHang21.Text.Replace(" ", "");
            if (this.txtSoHang11.Text.Length != this.txtSoHang21.Text.Length)
                return false;

            this.txtSoHang12.Text = this.txtSoHang12.Text.Replace(" ", "");
            this.txtSoHang22.Text = this.txtSoHang22.Text.Replace(" ", "");
            if (this.txtSoHang12.Text.Length != this.txtSoHang22.Text.Length)
                return false;

            this.txtThuong1.Text = this.txtThuong1.Text.Replace(" ", "");
            this.txtThuong2.Text = this.txtThuong2.Text.Replace(" ", "");
            if (this.txtThuong1.Text.Length != this.txtThuong2.Text.Length)
                return false;

            if (type == 3)
            {
                if (this.txtSoBiChia1.Text == "") return false;
                if (this.txtSoBiChia2.Text == "") return false;
                if (this.txtSoChia1.Text == "") return false;
                if (this.txtSoChia2.Text == "") return false;
                if (this.txtThuong1.Text == "") return false;
                if (this.txtThuong2.Text == "") return false;
            }
            else
            {
                if(this.txtSoHang11.Text == "") return false;
                if(this.txtSoHang12.Text == "") return false;
                if(this.txtSoHang21.Text == "") return false;
                if(this.txtSoHang22.Text == "") return false;
                if(this.txtKetQua1.Text == "") return false;
                if (this.txtKetQua2.Text == "") return false;
            }

            return true;
        }

        public void GetData()
        {
            originList = "";   
            if (type == 3)
            {
                originList += MatchingData(this.txtSoChia1.Text, this.txtSoChia2.Text);
                originList += "!";
                originList += MatchingData(this.txtThuong1.Text, this.txtThuong2.Text);
                originList += "!";
                string[] str1 = this.txtSoBiChia1.Lines;
                string[] str2 = this.txtSoBiChia2.Lines;
                for (int i = 0; i < str1.Length; i++)
                {
                    originList += MatchingData(str1[i], str2[i]);
                    originList += "!";
                }
            }
            else
            {
                originList += MatchingData(this.txtSoHang11.Text, this.txtSoHang21.Text);
                originList += "!";
                originList += MatchingData(this.txtSoHang12.Text, this.txtSoHang22.Text);
                originList += "!";
                string[] str1 = this.txtKetQua1.Lines;
                string[] str2 = this.txtKetQua2.Lines;
                for (int i = 0; i < str1.Length; i++)
                {
                    originList += MatchingData(str1[i], str2[i]);
                    originList += "!";
                }
            }
            list = " ";
            answer = " ";
        }

        private string MatchingData(string str1, string str2)
        {
            char charHide = '?';
            string result = "";
            for (int i = 0; i < str1.Length; i++)
            {
                if (str2[i] == charHide)
                {
                    result += "?" + "(" + str1[i] + ")";
                }
                else
                {
                    result += str2[i];
                }
            }
            return result;
        }

        public void InputData(string _originList, string _list, string _answer, int _type, int _level, int _open)
        {
            Reset();
            this.cbType.SelectedIndex = _type;
            this.cbType.Enabled = false;
            this.btnClone.Enabled = false;

            _originList = SmoothData(_originList);
            this.originList = _originList;
            this.list = _list;
            this.answer = _answer;
            this.Type = _type;
            this.Level = _level;
            this.Open = _open;

            string[] str = this.OriginList.Split('!');
            if (str.Length < 2)
            {
                return;
            }
            if (this.Type == 3)
            {
                this.txtSoChia1.Text = LayDuLieuDung(str[0]);
                this.txtThuong1.Text = LayDuLieuDung(str[1]);
                this.txtSoChia2.Text = LayDuLieuAn(str[0]);
                this.txtThuong2.Text = LayDuLieuAn(str[1]);
                for (int i = 2; i < str.Length; i++)
                {
                    this.txtSoBiChia1.Text += LayDuLieuDung(str[i]);
                    this.txtSoBiChia1.Text += "\n";
                    this.txtSoBiChia2.Text += LayDuLieuAn(str[i]);
                    this.txtSoBiChia2.Text += "\n";
                }
            }
            else
            {
                this.txtSoHang11.Text = LayDuLieuDung(str[0]);
                this.txtSoHang12.Text = LayDuLieuDung(str[1]);
                this.txtSoHang21.Text = LayDuLieuAn(str[0]);
                this.txtSoHang22.Text = LayDuLieuAn(str[1]);

                for (int i = 2; i < str.Length; i++)
                {
                    this.txtKetQua1.Text += LayDuLieuDung(str[i]);
                    this.txtKetQua1.Text += "\n";
                    this.txtKetQua2.Text += LayDuLieuAn(str[i]);
                    this.txtKetQua2.Text += "\n";
                }
            }            
        }

        private string LayDuLieuDung(string str)
        {
            string result = "";
            for (int i = 0; i < str.Length; i++)
            {
                if(CheckIsInt(str[i]))
                {
                    result += str[i];
                }
            }
            return result;
        }

        private string LayDuLieuAn(string str)
        {
            string result = "";
            bool adding = true;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '(')
                {
                    adding = false;
                }
                if (adding)
                {
                    if (str[i] == '?')
                    {
                        result += "?";
                    }
                    else
                    {
                        result += str[i];
                    }
                }
                if (str[i] == ')')
                {
                    adding = true;
                }

            }
            return result;
        }

        private bool CheckIsInt(char c)
        {
            if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
            {
                return true;
            }
            return false;
        }

        private string SmoothData(string str1)
        {
            bool smooth = false;
            int counter = 0;
            for (int i = str1.Length - 1; i > 0; i--)
            {
                if (str1[i] == '!')
                {
                    smooth = true;
                    counter++;
                }
                else
                {
                    break;
                }
            }
            if (smooth)
            {

                str1 = str1.Substring(0, str1.Length - counter);
            }
            return str1;
        }
    }
}
